import { Component, OnInit } from '@angular/core';
import { Teacher } from '../../model/teacher';
import { CourseService } from '../../service/course.service';

@Component({
  selector: 'app-teacher',
  templateUrl: './teacher.component.html',
  styleUrls: ['./teacher.component.css']
})
export class TeacherComponent implements OnInit {

  t:Teacher
  tl:Array<Teacher>
  constructor(private courseservice:CourseService) {
    this.t=new Teacher();
   }

   ngOnInit() {
    this.loadTeacherList();
  }

  save(){
    if(this.t.id){
      this.update()
    }
    else{
    this.addTeacher()
    }
  }

  addTeacher(){
    this.courseservice.addTeacher(this.t).then((flag:boolean)=>{
      if(flag==true){
        this.t=new Teacher();
        this.loadTeacherList();
      }
      else{
        alert("添加异常，请重试")
      }
    })
  }

  loadTeacherList(){
    this.tl= new Array();
    this.courseservice.getTeacherList()
    .then((data:any)=>{

      this.tl = data;


    })
  }

  toAdd(){
    this.t=new Teacher();
  }

  toupdate(x:Teacher){
    this.t=x;
  }

  update(){
    this.courseservice.updateTeacher(this.t).then((flag:boolean)=>{
      if(flag==true){
        this.t=new Teacher();
        this.loadTeacherList();
      }
      else{
        alert("修改异常，请重试");
      }
    })
  }

  deleteTeacher(x:Teacher){
    this.courseservice.deleteTeacher(x).then((flag:boolean)=>{
      if(flag==true){
        
        this.loadTeacherList();
      }
      else{
        alert("删除异常，请重试");
      }
    })
  }
}
